package com.cc.visitor.config;

import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfigure implements WebMvcConfigurer {
    @Value("${file.signature.new-word-path}")
    private String newWordPath;// 生成的word文件所在目录
    @Value("${file.signature.word-uri}")
    private String wordUri;// 下载word文件的uri路径
    @Value("${file.kaptcha.img-path}")
    private String kaptchaImgPath;
    @Value("${file.kaptcha.virtual-path}")
    private String kaptchaVirtualPath;
    @Value("${file.signature.img-path}")
    private String signatureImgPath;
    @Value("${file.signature.virtual-path}")
    private String signatureVirtualPath;

    /**
     * 使用 MDC 实现链路跟踪
     * @param registry
     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // centos7 中按照下面一行代码的方式需要在配置文件中设置图片文件的绝对路径：/projs/visitor/kaptcha/
        // 如果下面一行中是：file:/ ，则配置文件中图片的绝对路径是：projs/visitor/kaptcha/
        // 对于 windows 系统匹配下面代码的文件路径是：/d:/temp/kaptcha/
        registry.addResourceHandler(kaptchaVirtualPath).addResourceLocations("file:" + kaptchaImgPath);// 验证码图片
        registry.addResourceHandler(signatureVirtualPath).addResourceLocations("file:" + signatureImgPath);// 用户制作访单前签名的图片
        registry.addResourceHandler(wordUri).addResourceLocations("file:" + newWordPath);// 下载生成的word文件的uri
    }

    @Bean
    public TomcatServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
                connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
            }
        });
        return factory;
    }
}
